//
//  YZPigGroupDocHealthDetailVC.m
//  VET
//
//  Created by YZ on 2018/7/30.
//  Copyright © 2018年 YZ. All rights reserved.
//

#import "YZPigGroupDocHealthDetailVC.h"
#import "YZBaseRefreshTableView.h"
#import "YZPigGroupDocHealthDetailViewModel.h"
#import "YZPigGroupDocHealthDetailCell.h"
#import "YZPigGroupDocHealthDetailReamrkCell.h"

@interface YZPigGroupDocHealthDetailVC ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) YZPigGroupDocHealthDetailViewModel *viewModel;

@property (nonatomic, strong) YZBaseRefreshTableView *contentTb;

@end

@implementation YZPigGroupDocHealthDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self configUI];
    [self addObserver];
    [self.contentTb headerBeginRefresh];
}

- (void)configUI
{
    self.title = [NSString stringWithFormat:@"猪群%@",self.model.batchNo];
    self.view.backgroundColor = kColorWithHex(0xE9EAEB);
    [self.view addSubview:self.contentTb];
    
    self.navigationItem.rightBarButtonItem = [self barButtonItemWithTitle:@"删除" titleColor:kColorWithHex(0x32BE60) target:self action:@selector(deleteClicked)];
    
    YZWeakSelf(self);
    self.contentTb.headerRefreshBlock = ^{
        [weakself.viewModel headerRefreshSuccess:^(id obj) {
            [weakself.contentTb reloadData];
            [weakself.contentTb headerEndRefresh];
        } fail:^(NSError *error) {
            [weakself.contentTb headerEndRefresh];
        }];
    };
}

- (void)addObserver
{
    [RACObserve(self.viewModel, loading) subscribeNext:^(id  _Nullable x) {
        if ([x boolValue]) {
            [SVProgressHUD show];
        }else
        {
            [SVProgressHUD dismiss];
        }
    }];
}

- (void)deleteClicked
{
    [[NSNotificationCenter defaultCenter] postNotificationName:kHealthRefreshNotification object:nil];
}

#pragma mark -- UITableViewDelegate,UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.viewModel.dataSource.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.viewModel.dataSource[section].count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell;
    if (indexPath.section != self.viewModel.dataSource.count - 1) {
        static NSString *cellId = @"YZPigGroupDocHealthDetailCell";
        cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if(!cell){
            cell = [[YZPigGroupDocHealthDetailCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
        }
        id obj = [self.viewModel modelInSection:indexPath.section row:indexPath.row];
        ((YZPigGroupDocHealthDetailCell *)cell).model = obj;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else
    {
        static NSString *cellId = @"YZPigGroupDocHealthDetailReamrkCell";
        cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if(!cell){
            cell = [[YZPigGroupDocHealthDetailReamrkCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
        }
        id obj = [self.viewModel modelInSection:indexPath.section row:indexPath.row];
        ((YZPigGroupDocHealthDetailReamrkCell *)cell).model = obj;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewAutomaticDimension;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section >= 1) {
        return kCurrentDeviceNum(30);
    }else
    {
        return 0.01f;
    }
    return 0.01f;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section >= 1) {
        UIView *bgView = [[UIView alloc] init];
        bgView.backgroundColor = kColorWithHex(0xE9EAEB);
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(kCurrentDeviceNum(10), kCurrentDeviceNum(10), tableView.contentSize.width, kCurrentDeviceNum(20))];
        titleLabel.textAlignment = NSTextAlignmentLeft;
        titleLabel.font = kCurrentDeviceFont(14);
        titleLabel.text = section != self.viewModel.dataSource.count - 1?[NSString stringWithFormat:@"药物%ld",section]:@"备注";
        titleLabel.textColor = kColorWithHex(0x7A7A7A);
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(kCurrentDeviceNum(20), kCurrentDeviceNum(29), kScreenWidth, kCurrentDeviceNum(1))];
        lineView.backgroundColor = kColorWithHex(0xE9EAEB);
        [bgView addSubview:titleLabel];
        [bgView addSubview:lineView];
        
        return bgView;
    }
    return nil;
}

#pragma mark -- setters && getters

- (YZBaseRefreshTableView *)contentTb
{
    if(!_contentTb){
        _contentTb = [[YZBaseRefreshTableView alloc] initWithFrame:CGRectMake(0, kCurrentDeviceNum(20), kScreenWidth, kScreenHeight - kTopViewHeight - YZ_STATUSBAR_HEIGHT - kCurrentDeviceNum(20) - YZ_BOTTOM_MARGIN) style:UITableViewStylePlain];
        _contentTb.tableFooterView = [UIView new];
        _contentTb.delegate = self;
        _contentTb.dataSource = self;
        _contentTb.estimatedRowHeight = 30;
        _contentTb.estimatedSectionHeaderHeight = 0;
        _contentTb.estimatedSectionFooterHeight = 0;
    }
    return _contentTb;
}

- (YZPigGroupDocHealthDetailViewModel *)viewModel
{
    if(!_viewModel){
        _viewModel = [YZPigGroupDocHealthDetailViewModel new];
        _viewModel.model = self.model;
        _viewModel.rid = self.healthModel.rid;
    }
    return _viewModel;
}

@end
